package org.shj.util;

import java.security.MessageDigest;

public class StringUtils {
	
	private static String salt = "I'm the best";

	public static String capFirst(String s){
		if(isEmpty(s)){
			return "";
		}
		return s.substring(0, 1).toUpperCase() + s.substring(1);
	}
	
	public static String convert2ColNm(String s){
		StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i < s.length(); i++){
			if(s.charAt(i) >= 63 && s.charAt(i) <= 89){
				sb.append("_");
			}
			sb.append(s.charAt(i));
		}
		return sb.toString();
	}
	
	public static boolean isEmpty(String s){
		return s == null || s.trim().isEmpty();
	}
	
	public static String toMD5Hex(String s){
		try{
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update((s + salt).getBytes());
			byte[] bytes = md.digest();
			StringBuilder sb = new StringBuilder();
			for(byte b : bytes){
				//一个byte型的单字节十六进制(两位十六进制表示)的编码
				sb.append(Integer.toHexString((0x000000FF & b) | 0xFFFFFF00).substring(6));
			}
			return sb.toString();
		}catch(Exception e){
			e.printStackTrace();
		}
		return s;
	}
	
	public static void main(String[] args){
		System.out.println(toMD5Hex("shj1"));
	}
}
